<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>A Client-Server</title>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

</head>

<body>

<div id="chat">
<h2>Chat</h2>
<form method="Get" id="chatForm">
  Message: <input type="text" name="message" id="message" value="Hello World">
  <input type="submit" value="Submit">
</form>
<pre id="log"></pre>
</div>

<script type="text/javascript" src="http://self:8084/static/client-server.js"></script>
<script type="text/javascript">
window.__LOCAL__ = true;

$(document).ready(function () {

    var log$ = $("#log");
	function log(msg) {
		log$.text(""+msg + "\n" + log$.text())
	}
	
	var server = new Addressable.Server({ useGears: true });

	server.onmessage = function (message) {
		log(message)
	}

	var scope = location.href;

	server.connect(function (id, url) {
	    log("Connected.")
	    server.subscribe(scope, function () { 
		    log("Joined channnel")
		})

		setInterval(function () { // renew subscription
			server.subscribe(scope, function () {})
		}, 1000 * 4)
	})
	
	var message = $("#message").focus();

	$("#chatForm").submit(function (e) {
		server.post(scope, message.val())
		message.val("")
		e.preventDefault()
	})

})



</script>

</body>
</html>
